package org.server.task;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.*;



public  class ServerStatusListener implements ServletContextListener {
  Timer timer;
  String strPlatformPrefix = "cmd.exe /c ";
  String strMySQLFolder = "C:/DEV/installations/UniServer/usr/local/mysql/bin/";
  String strBackupFolder = "C:/DEV/dbbackup/";
  String strBackupPrefix = "dbname";
  String strBackupNameDateFormat = "dd-MM-yyyy-HH-mm";
  String strAppName = "mysqldump.exe";
  String strUserName = "root";
  String strPassword = "root";
  String strDbNameToBackup = "dbname";
  int intMinutes = 180;

  /*This method is invoked when the Web Application has been removed 
  and is no longer able to accept requests
  */
  public void contextDestroyed(ServletContextEvent event)
  {
    //Output a simple message to the server's console
    System.out.println("The Database MYSQL Backup Is Ready Has Been Removed");
  }


  //This method is invoked when the Web Application
  //is ready to service requests
  public void contextInitialized(ServletContextEvent event)
  {
    System.out.println("The Database MYSQL Backup Is Ready");
    
    // Load Properties
    ServletContext servletContext = event.getServletContext();
    String strPath = servletContext.getRealPath("/WEB-INF/");
    Properties prop = new Properties();
    //load Properties prop from a file
    try {
        prop.load(new FileInputStream(strPath + "/" + "backup.properties"));
        //get values for backup
        strPlatformPrefix = prop.getProperty("strPlatformPrefix");
        strMySQLFolder = prop.getProperty("strMySQLFolder");
        strBackupFolder = prop.getProperty("strBackupFolder");
        strBackupPrefix = prop.getProperty("strBackupPrefix");
        strAppName = prop.getProperty("strAppName");
        strUserName = prop.getProperty("strUserName");
        strPassword = prop.getProperty("strPassword");
        strDbNameToBackup = prop.getProperty("strDbNameToBackup");
        strBackupNameDateFormat  = prop.getProperty("strBackupNameDateFormat");
        intMinutes = Integer.parseInt(prop.getProperty("intMinutes"));
        
    } catch (IOException e) {
        System.out.println("IOException :: "+e);
    }catch (Exception e) {
    	System.out.println(e);
	}


    int intSeconds = 60;
    timer = new Timer();
    timer.schedule(new CreateBackup(), 0, intMinutes*intSeconds*1000);
  }
  
  class CreateBackup extends TimerTask {
  	
      public void run() {
			try {
				Date date = new Date();
		        java.text.DateFormat df = new SimpleDateFormat(strBackupNameDateFormat);
		        String strdate = df.format(date);
		        
				// String strcommand = "cmd.exe /c C:/DEV/installations/UniServer/usr/local/mysql/bin/mysqldump.exe --user root --password=root --opt dbname > C:/DEV/dbbackup/dbname"+ strdate +".sql";//working
		        String strcommand = strPlatformPrefix + strMySQLFolder + strAppName + " --user "+ strUserName +" --password=" + strPassword +" --opt " + strDbNameToBackup + " > " + strBackupFolder + strBackupPrefix + strdate +".sql";
				System.out.println(strcommand);// for debug purposes
				Runtime.getRuntime().exec(strcommand);//
				System.out.println("OK");// for debug purposes
			} catch (IOException e) {
				System.err.println(e.toString());
			} catch (Exception e) {
				System.err.println(e.toString());
			}
      }
  }
}
